<?php
class CarModule{
	function index(){
		global $_G;
		//买卖技巧
		$jiqiao=$_G['db']->fetch_all("SELECT title,aid,default_image,summary FROM".table('article')."WHERE  status=1 AND cid=8 ORDER BY istop DESC,iscommend DESC, dateline DESC limit 5");
		$jiqiao_top=array_shift($jiqiao);//买卖技巧置顶头条
		$top=$_G['db']->fetch_all("SELECT title,aid,dateline FROM".table('article')." WHERE  status=1 ORDER BY istop DESC,iscommend DESC, dateline DESC limit 10");
		$img2=$_G['db']->fetch_all("SELECT title,aid,dateline,default_image FROM".table('article')." WHERE status=1 AND  default_image!='' ORDER BY views DESC limit 5");
		foreach($img2 as $v){
			$img.=$mar.'{"p":"'.DATA_DIR.'files/'.$v['default_image'].'","l":"'.url('index.php?m=car&f=news&id='.$v['aid']).'","t":"'.cut_str($v['title'],38,'').'"}';
			$mar=',';
		}		
		include template('index');
	}
	
	function buy(){
		global $_G;
		if(getGP('id','G','int')) {
			$biansu=array(1=>'手动',2=>' 自动 ',3=>'手自一体',4=>'无级变速');
			$data=$_G['db']->fetch_one_array("SELECT a.*,c.catename,d.catename as brand FROM".table('car_cars')."a LEFT JOIN ".table('category')."c ON a.cid=c.cid  LEFT JOIN ".table('category')."d ON a.bid=d.cid WHERE a.status=1 AND a.id='".getGP('id','G','int')."'");
			if($data['id']){
				$files=$_G['db']->fetch_all("SELECT * FROM".table('files')."WHERE type=1 AND referid=".$data['id']);
				$_G['db']->update("car_cars",array('views'=>$data['views']+1),array('id'=>getGP('id','G','int')));
			}
			include template('buy_view');
		}else{
			$bid=getGP('bid','G','int');
			$pri=getGP('pri','G','int');
			$pris=getGP('pris','G','int');
			$cid=getGP('cid','G','int');
			$yr =getGP('yr','G','int');
			$pl =getGP('pl','G','int');
			$lic=getGP('lic','G','int');
			$bs =getGP('bs','G','int');
			$sid =getGP('sid','G','int');
			if( $bid ) $where.=" AND bid=$bid";
			if( $cid ) $where.=" AND cid=$cid";
			if( $bs ) $where.=" AND biansu=$bs";
			if( $sid ){
				switch($sid){
					case 1: $where.=" AND shopid < 1";break;
					case 2: $where.=" AND shopid >0 ";break;
				}
			} 
			if( $pri ){
				switch($pri){
					case 1: $where.=" AND price < 3";break;
					case 2: $where.=" AND price >= 3 AND price <= 5";break;
					case 3: $where.=" AND price > 5 AND price <= 10";break;
					case 4: $where.=" AND price > 10 AND price <= 15";break;
					case 5: $where.=" AND price > 15 AND price <= 20";break;
					case 6: $where.=" AND price > 20 AND price <= 50";break;
					case 7: $where.=" AND price > 50";break;
				}
			} 
			if( $lic ){
				switch($lic){
					case 1: $where.=" AND licheng < 1";break;
					case 2: $where.=" AND licheng >= 1 AND licheng<= 3";break;
					case 3: $where.=" AND licheng > 3 AND licheng <= 6";break;
					case 4: $where.=" AND licheng > 6 AND licheng <= 10";break;
					case 5: $where.=" AND licheng > 10";break;
				}
			} 
			if( $yr ){
				$now=gdate('Y',TIMESTAMP);
				$year1=strtotime($now-1);
				$year3=strtotime($now-3);
				$year6=strtotime($now-6);
				$year10=strtotime($now-10);
				switch($yr){
					case 1: $where.=" AND shangpai > $year1";break;
					case 2: $where.=" AND shangpai >= $year3 AND shangpai<= $year1";break;
					case 3: $where.=" AND shangpai > $year6 AND shangpai <= $year3";break;
					case 4: $where.=" AND shangpai > $year10 AND shangpai <= $year6";break;
					case 5: $where.=" AND shangpai < $year10";break;
				}
			} 
			if( $pl ){
				switch($pl){
					case 1: $where.=" AND pailiang < 1";break;
					case 2: $where.=" AND pailiang >= 1 AND pailiang<= 1.6";break;
					case 3: $where.=" AND pailiang > 1.6 AND pailiang <= 2";break;
					case 4: $where.=" AND pailiang > 2 AND pailiang <= 2.6";break;
					case 5: $where.=" AND pailiang > 2.6";break;
				}
			} 
			switch($pris){
				case 1: $order=' ORDER BY price DESC '; break;
				case 2: $order=' ORDER BY price ASC '; break;
				default : $order=' ORDER BY id DESC ';
			}
			$query=$_G['db']->page_query("SELECT * FROM".table('car_cars')."WHERE  status=1   $where $order",9);
			while($rs=$_G['db']->fetch_array($query)){
				$datas[]=$rs;
			}
			$num=$_G['db']->result("SELECT count(id) FROM".table('car_cars')."WHERE  status=1  $where ");
			$page=pager($num,9);
			include template('buy');
		}
	}
	
	function addcar(){
		global $_G;
		if(check_submit('fabu')){
			$data=array(
				'cid'=>getGP('cid','P','int'),
				'bid'=>getGP('brand2','P','int'),
				'title'=>getGP('title','P'),
				'price'=>getGP('price','P','float'),
				'contact'=>getGP('contact','P'),
				'phone'=>getGP('phone','P'),
				'jianjie'=>getGP('jianjie','P'),
				'color'=>getGP('color','P'),
				'biansu'=>getGP('biansu','P'),
				'licheng'=>getGP('licheng','P'),
				'pailiang'=>getGP('pailiang','P'),
				'created'=>TIMESTAMP,
				'shangpai'=>strtotime(getGP('shangpaiy','P').'-'.getGP('shangpaim','P'))
			);
			foreach($data as $k=>$v){
				if(!$v){
					$error=$k;break;
				}
			}
			if($error && $k!="price"){
				error('请填写完整信息');
			}
			if(ican('addcar')) {
				$data['id']=getGP('id','P','int');
			}
			$imagefid=getGP('default_image','P','int');
			if($imagefid > 0){
				$data['picture']= make_thumb($imagefid);
			}
			$data['description']=getGP('description','P');
			$data['content']=getGP('content','P');
			$data['uid']=$_G['uid'];
			if(!$data['id'] ) {
				$id=$_G['db']->insert('car_cars',$data,true);
			}else{
				$_G['db']->update('car_cars',$data,array('id'=>$data['id']));
				$id=$data['id'];
			}
			//检查文章附件
			$attachments = getGP('admin_upload_attachments','C');
			if ( !empty($attachments) && preg_match('/^([0-9]+(,[0-9])?)+$/', $attachments) ) {
				$_G['db']->query("UPDATE ".DB_PREFIX."files SET type=1,referid = $id WHERE fid IN ($attachments)");
				set_cookie('admin_upload_attachments');
			}
			if($data['id']){
				error('编辑成功.',url('index.php?m=car&f=buy'),true,5000);
			}else{
				error('发布成功,请耐心等到审核,审核通过后可以在下面地址里查看到你的信息.',url('index.php?m=car&f=buy'),true,5000);
			}
		}else{
			$id=getGP('id','G','int');
			if($id && ican('addcar')){
				$data=$_G['db']->fetch('car_cars','*',array('id'=>$id));
				if($data['description'])$data['descriptions']=explode(',',$data['description']);
				if(is_array($data['descriptions'])){
					foreach($data['descriptions'] as $k=>$v){
						if(trim($v))$data['descriptions'][$k]=trim($v);
					}
				}
				$attachments = $_G['db']->fetch_all("SELECT fid,originalname,filepath,thumb,filesize,filetype,fileext,dateline FROM ".DB_PREFIX."files WHERE referid = '$id' AND type = 1");
			}
			include template('add');
		}
	}
	
	function qiugouadd(){
		global $_G;
		if(check_submit('fabuq')){
			$data=array(
				'title'=>getGP('title','P'),
				'price'=>getGP('price','P','float'),
				'contact'=>getGP('contact','P'),
				'phone'=>getGP('phone','P'),
				'created'=>TIMESTAMP,
			);
			foreach($data as $k=>$v){
				if(!$v){
					$error=$k;break;
				}
			}
			if($error && $k!="price"){
				error('请填写完整信息');
			}
			if(ican('addcar')){
				$data['id']=getGP('id','P','int');
			}
			$data['color']=getGP('color','P');
			$data['age']=getGP('age','P','int');
			$data['bid']=getGP('brand2','P','int');
			$data['uid']=$_G['uid'];
			
			$data['description']=getGP('description','P');
			if($data['id']){
				$_G['db']->update('car_qiugou',$data,array('id'=>$data['id']));
			}else{
				$_G['db']->insert('car_qiugou',$data);
			}
			if($data['id']){
				error('编辑成功',url('index.php?m=car&f=qiugou'),true,5000);
			}else{
				error('发布成功,请耐心等到审核,审核通过后可以在下面地址里查看到你的信息.',url('index.php?m=car&f=qiugou'),true,5000);
			}
		}else{
			$id=getGP('id','G','int');
			if($id && ican('addqiugou')){
				$data=$_G['db']->fetch('car_qiugou','*',array('id'=>$id));
			}
			include template('qiugouadd');
		}
	}
	function sell(){
		global $_G;
		if(getGP('id','G','int')) {
			
		}else{
			include template('sell');
		}
	}
	
	function qiugou(){
		global $_G;
		$age=array(1=>'1年以内',2=>'1-3年 ',3=>'3-5年',4=>'5-8年',5=>'8年以上');
		if(getGP('id','G','int')) {
			$data=$_G['db']->fetch_one_array("SELECT a.*,d.catename FROM".table('car_qiugou')."a LEFT JOIN ".table('category')."d ON a.bid=d.cid WHERE  a.status=1 AND  a.id='".getGP('id','G','int')."'");
			$_G['db']->update("car_qiugou",array('views'=>$data['views']+1),array('id'=>getGP('id','G','int')));
			
			include template('qiugou_view');
		}else{
			$bid=getGP('bid','G','int');
			$pri=getGP('pri','G','int');
			$yr =getGP('yr','G','int');
			$pris=getGP('pris','G','int');
			if( $bid ) $where.=" AND q.bid=$bid";
			if( $pri ){
				switch($pri){
					case 1: $where.=" AND q.price < 3";break;
					case 2: $where.=" AND q.price >= 3 AND q.price <= 5";break;
					case 3: $where.=" AND q.price > 5 AND q.price <= 10";break;
					case 4: $where.=" AND q.price > 10 AND q.price <= 15";break;
					case 5: $where.=" AND q.price > 15 AND q.price <= 20";break;
					case 6: $where.=" AND q.price > 20 AND q.price <= 50";break;
					case 7: $where.=" AND q.price > 50";break;
				}
			}
			if( $yr ){
				$where.=" AND q.age=$yr";
			}  
			switch($pris){
				case 1: $order=' ORDER BY price DESC '; break;
				case 2: $order=' ORDER BY price ASC '; break;
				default : $order=' ORDER BY id DESC ';
			}
			$query=$_G['db']->page_query("SELECT q.*,c.catename FROM".table('car_qiugou')."q LEFT JOIN".table('category')."c ON q.bid=c.cid WHERE  q.status=1   $where $order",9);
			while($rs=$_G['db']->fetch_array($query)){
				$datas[]=$rs;
			}
			$num=$_G['db']->result("SELECT count(id) FROM".table('car_qiugou')."q WHERE  q.status=1   $where ");
			$page=pager($num,9);
			include template('qiugou');
		}
	}
	
	function pinggu(){
		global $_G;
		if(getGP('id','G','int')) {
			$data=$_G['db']->fetch_one_array("SELECT a.* FROM".table('car_pinggu')."a  WHERE  a.status=1 AND  a.id='".getGP('id','G','int')."'");
			$_G['db']->update("car_pinggu",array('views'=>$data['views']+1),array('id'=>getGP('id','G','int')));
			
			include template('pinggu_view');
		}else{
			$bid=getGP('bid','G','int');
			$pri=getGP('pri','G','int');
			$yr =getGP('yr','G','int');
			$pris=getGP('pris','G','int');
			switch($pris){
				case 1: $order=' ORDER BY price DESC '; break;
				case 2: $order=' ORDER BY price ASC '; break;
				default : $order=' ORDER BY id DESC ';
			}
			$query=$_G['db']->page_query("SELECT q.* FROM".table('car_pinggu')."q  WHERE status=1 $where $order",9);
			while($rs=$_G['db']->fetch_array($query)){
				$datas[]=$rs;
			}
			$num=$_G['db']->result("SELECT count(id) FROM".table('car_pinggu')."q WHERE status=1 $where ");
			$page=pager($num,9);
			include template('pinggu');
		}
	}
	
	function news(){
		global $_G;
		if(getGP('id','G','int')) {
			$data=$_G['db']->fetch_one_array("SELECT a.*,c.catename FROM".table('article')."a LEFT JOIN ".table('category')."c ON a.cid=c.cid WHERE a.status=1 AND  a.aid='".getGP('id','G','int')."'");
			
			$_G['db']->update("article",array('views'=>$data['views']+1),array('aid'=>getGP('id','G','int')));
			include template('news_view');
		}elseif(getGP('cid','G','int')){
			$cate=$_G['db']->fetch_one_array("SELECT cid,catename FROM".table('category')."WHERE cid='".getGP('cid','G','int')."'");
			$query=$_G['db']->page_query("SELECT title,aid,dateline FROM".table('article').'WHERE status=1 AND  cid="'.getGP('cid','G','int').'" ORDER BY dateline DESC',10);
			while($rs=$_G['db']->fetch_array($query)){
				$datas[]=$rs;
			}
			$num=$_G['db']->result("SELECT count(aid) FROM".table('article').'WHERE status=1 AND  cid="'.getGP('cid','G','int').'"');
			$page=pager($num,10);
			include template('news_list');
		}else{
			//买卖技巧
			$jiqiao=$_G['db']->fetch_all("SELECT title,aid,default_image,summary FROM".table('article')."WHERE  status=1 AND cid=8 ORDER BY istop DESC,iscommend DESC, dateline DESC limit 5");
			$jiqiao_top=array_shift($jiqiao);//买卖技巧置顶头条
			//行业新闻
			$hangye=$_G['db']->fetch_all("SELECT title,aid,dateline FROM".table('article')."WHERE  status=1 AND cid=1 ORDER BY istop DESC,iscommend DESC, dateline DESC limit 10");
			//车市行情
			$cheshi=$_G['db']->fetch_all("SELECT title,aid,dateline FROM".table('article')."WHERE  status=1 AND cid=5 ORDER BY istop DESC,iscommend DESC, dateline DESC limit 10");
			//二手导购
			$daogou=$_G['db']->fetch_all("SELECT title,aid,dateline FROM".table('article')."WHERE  status=1 AND cid=6 ORDER BY istop DESC,iscommend DESC, dateline DESC limit 10");
			//数据报告
			$baogao=$_G['db']->fetch_all("SELECT title,aid,dateline FROM".table('article')."WHERE  status=1 AND cid=4 ORDER BY istop DESC,iscommend DESC, dateline DESC limit 10");
			//汽车评测
			$pingce=$_G['db']->fetch_all("SELECT title,aid,dateline FROM".table('article')."WHERE  status=1 AND cid=7 ORDER BY istop DESC,iscommend DESC, dateline DESC limit 10");
			//名家专栏
			$mingjia=$_G['db']->fetch_all("SELECT title,aid,dateline FROM".table('article')."WHERE  status=1 AND cid=3 ORDER BY istop DESC,iscommend DESC, dateline DESC limit 10");
			//人物访问
			$renwu=$_G['db']->fetch_all("SELECT title,aid,dateline FROM".table('article')."WHERE  status=1 AND cid=2 ORDER BY istop DESC,iscommend DESC, dateline DESC limit 10");
			
			$top=$_G['db']->fetch_all("SELECT title,aid,dateline FROM".table('article')." WHERE status=1 ORDER BY istop DESC,iscommend DESC, dateline DESC limit 10");
			
			$img2=$_G['db']->fetch_all("SELECT title,aid,dateline,default_image FROM".table('article')." WHERE  status=1 AND default_image!='' ORDER BY views DESC limit 5");
			foreach($img2 as $v){
				$img.=$mar.'{"p":"'.DATA_DIR.'files/'.$v['default_image'].'","l":"'.url('index.php?m=car&f=news&id='.$v['aid']).'","t":"'.$v['title'].'"}';
				$mar=',';
			}
			include template('news');
		}
	}
	
	function shangjia(){
		global $_G;
		if(getGP('id','G','int')) {
			
		}else{
			include template('shangjia');
		}
	}
	
	function ajbrand(){
		global $_G;
		if(!getGP('inajax')) return ;
		
		$id=getGP('id','G','int');
		$data['s']=false;
		if($id){
			$sql="SELECT cid,catename FROM".table('category')."WHERE type=2 AND pid=".$id;
			$data['d']=$_G['db']->fetch_all($sql);
			if(!empty($data['d']))$data['s']=true;
		}
		echo json_encode($data);exit;
	}
	function gonglei(){
		global $_G;
		if(getGP('id','G','int')) {
			
		}else{
			$faq=$_G['db']->fetch('page','*',array('id'=>10));
			include template('gonglei');
		}
	}
	function to_front(){
		global $_G;
		$id = getGP('id','G','int');
		$aid = getGP('aid','G','int');
		$thumb = make_thumb($id);
		if(!ISADMIN){
			$_G['db']->update('car_cars',array('picture'=>$thumb),array('id'=>$aid,'uid'=>$_G['uid']));
		}else{
			$_G['db']->update('car_cars',array('picture'=>$thumb),array('id'=>$aid));
		}
		exit('success');
	}
	function del_front(){
		global $_G;
		
		$aid = getGP('aid','G','int');
		if(!ISADMIN){
			$_G['db']->update('car_cars',array('picture'=>''),array('id'=>$aid,'uid'=>$_G['uid']));
		}else{
			$_G['db']->update('car_cars',array('picture'=>''),array('id'=>$aid));
		}
		exit('success');
	}
	
	function pingguadd(){
		global $_G;
		if(check_submit('fabup')){
			$data=array(
				'description'=>getGP('description','P'),
				'contact'=>getGP('contact','P'),
				'phone'=>getGP('phone','P'),
				'created'=>TIMESTAMP,
			);
			foreach($data as $k=>$v){
				if(!$v){
					$error=$k;break;
				}
			}
			if($error){
				error('请填写完整信息');
			}
			if(ican('addpinggu')){
				$data['id']=getGP('id','P','int');
				if($data['id']) {
				$data['replay']=getGP('replay','P');
				$data['replayed']=TIMESTAMP;
				}
			}
			$data['uid']=$_G['uid'];
			if($data['id']) {
				$_G['db']->update('car_pinggu',$data,array('id'=>$data['id']));
			
			}else{
				$id = $_G['db']->insert('car_pinggu',$data);
			}
			if($data['id']){
				error('编辑成功',url('index.php?m=car&f=pinggu'),true,3000);
			}else{
				error('发布成功,请耐心等到评估,审核通过后可以在下面地址里查看到你的信息.',url('index.php?m=car&f=pinggu'),true,10000);
			}
		}else{
			$id=getGP('id','G','int');
			if($id && ican('addqiugou')){
				$data=$_G['db']->fetch('car_pinggu','*',array('id'=>$id));
			}else{
				$id='';
			}
			include template('pingguadd');
		}
	}
	function page(){
		global $_G;
		$id=getGP('id','G','int');
		if($id) {
			$data=$_G['db']->fetch('page','*',array('id'=>$id));
			include template('page_view');
		}else{
			include template('page');
		}
	}
}